package com.yjhb.android.sbz.v2.mvp.presenter

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.yjhb.android.sbz.http.HttpObserver
import com.yjhb.android.sbz.http.HttpTool
import com.yjhb.android.sbz.mvp.ConventionPresenter2
import com.yjhb.android.sbz.v2.mvp.view.V2RewardView


/**
 *  创建者 zx
 *  创建时间 2021/4/7 10:45
 */
class V2RewardPresenter<D, U>(view: V2RewardView<D, U>) : ConventionPresenter2<V2RewardView<D, U>>(view) {

    fun getRankList(rankType: Int, clazz: Class<D>) {
        view?.onLoadingList()
        model?.let {
            HttpTool.request(model.getNewRankList(rankType, 1), object : HttpObserver<JSONObject>() {
                override fun onResult(data: JSONObject) {
                    val rankAgent = data.getJSONArray("rankAgent")
                    val list = mutableListOf<D>()
                    for (rankItem in rankAgent.toJavaList(JSONObject::class.java)) {
                        list.add(JSON.toJavaObject(rankItem, clazz))
                    }
                    view?.onListSuccess(data.getString("period"), list)
                }

                override fun onError(code: Int, msg: String) {
                    view?.onFailure(1, code, msg)
                }
            })
        }
    }

    fun getNewRankList(rankType: Int, clazz: Class<D>) {
        view?.onLoadingList()
        model?.let {
            HttpTool.request(model.getNewRank(rankType, 1), object : HttpObserver<JSONObject>() {
                override fun onResult(data: JSONObject) {
                    val rankAgent = data.getJSONArray("rankAgent")
                    val list = mutableListOf<D>()
                    for (rankItem in rankAgent.toJavaList(JSONObject::class.java)) {
                        list.add(JSON.toJavaObject(rankItem, clazz))
                    }
                    view?.onListSuccess(data.getString("period"), list)
                }

                override fun onError(code: Int, msg: String) {
                    view?.onFailure(1, code, msg)
                }
            })
        }
    }



    fun getUserRankInfo(rankType: Int, clazz: Class<U>) {
        view?.onLoadingUserInfo()
        model?.let {
            HttpTool.request(it.getV2UserRankInfo(rankType), object : HttpObserver<JSONObject>() {
                override fun onResult(data: JSONObject) {
                    view?.onUserInfoSuccess(JSON.toJavaObject(data, clazz))
                }

                override fun onError(code: Int, msg: String) {
                    view?.onFailure(2, code, msg)
                }
            })
        }
    }

    fun getStageReward(stage: Int) {
        view?.receiving()
        model?.let {
            HttpTool.request(model.getStageReward(stage), object : HttpObserver<Any?>() {
                override fun onResult(data: Any?) {
                    view?.successReceived()
                }

                override fun onError(code: Int, msg: String) {
                    view?.onFailure(3, code, msg)
                }
            })
        }
    }


}